package com.ztsoft.user.service.dynamic.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ztsoft.user.mapper.dynamic.DynamicFormMapper;
import com.ztsoft.user.pojo.entity.dynamic.DynamicForm;
import com.ztsoft.user.pojo.enums.ResultCode;
import com.ztsoft.user.pojo.vo.FormInfo;
import com.ztsoft.user.pojo.vo.ResultData;
import com.ztsoft.user.pojo.vo.dynamic.DynamicFormVo;
import com.ztsoft.user.pojo.vo.dynamic.SearchVerifyVo;
import com.ztsoft.user.service.dynamic.DynamicFormService;
import com.ztsoft.user.service.dynamic.FormDetailService;
import com.ztsoft.user.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class DynamicFormServiceImpl extends ServiceImpl<DynamicFormMapper, DynamicForm> implements DynamicFormService {

    @Autowired
    private FormDetailService formDetailService;

    /**
     * 保存动态表单
     *
     * @param dynamicForm
     * @return
     */
    @Override
    public Result<Object> saveDynamicForm(DynamicForm dynamicForm) {
        Date date = new Date(System.currentTimeMillis());
        List<DynamicForm> list = this.list(new QueryWrapper<DynamicForm>()
                .eq("table_code", dynamicForm.getTableCode())
                .eq("form_type", dynamicForm.getFormType()));
        if (list.isEmpty()) {
            dynamicForm.setCreateTime(date);
            dynamicForm.setModifyTime(date);
            this.save(dynamicForm);
            return Result.success();
        } else {
            return Result.returnMsg("添加失败，同一表格下不允许添加同一类型的配置", ResultCode.FAILED.getCode());
        }
    }

    @Override
    public Result<Object> queryDynamicFormPage(DynamicForm dynamicForm, Integer currentPage, Integer pageSize) {
        Map<String, Object> resultMap = new HashMap<>();
        QueryWrapper<DynamicForm> queryWrapper = new QueryWrapper<>(dynamicForm);
        IPage<DynamicForm> page = this.page(new Page<>(currentPage, pageSize), queryWrapper);

        resultMap.put("count", page.getTotal());
        resultMap.put("data", page.getRecords());
        return Result.success(resultMap);
    }

    //表单相关
    @Override
    public void generateForm(ResultData resultData, String tableCode) {
        List<DynamicForm> list = this.list(new QueryWrapper<DynamicForm>().eq("table_code", tableCode));
        Map<String, List<SearchVerifyVo>> rules = new HashMap<>();
        FormInfo formInfo = new FormInfo();
        for (DynamicForm dynamicForm : list) {
            Map<String, Object> defaultValueMap = new HashMap<>();
            List<DynamicFormVo> formList = new ArrayList<>();
            formDetailService.generateFormDetail(formList, dynamicForm, rules, defaultValueMap);
            formInfo.put(dynamicForm.getFormType(), formList);
            if ("formList".equals(dynamicForm.getFormType())) {
                formInfo.setDefaultValue(defaultValueMap);
            } else if ("formSlotArr".equals(dynamicForm.getFormType())) {
                formInfo.setDefaultSlotValue(defaultValueMap);
            }
        }
        if (!list.isEmpty()) {
            formInfo.setLabelWidth(list.get(0).getLabelWidth());
        }

        formInfo.setRules(rules);
        resultData.setFormConfig(formInfo);
    }
}
